<?php
/**
 * Tag表的Logic
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/27
 * Time: 11:13
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\Tag;
use App\Http\Logics\BaseLogic;
class TagLogic extends BaseLogic
{
    public static function createTag($data)
    {
        $tag = new Tag();
        $tag->name = $data['name'];
        $tag->parent_id = $data['parent_id'];
        if (!$tag->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function getTagList($data)
    {
        $tag = Tag::with(['tag'])->where(function($query) use ($data){
            if (isset($data['parent_id']) && $data['parent_id'] !== ''){
                $query->where('parent_id', $data['parent_id']);
            }
            if (isset($data['category_id']) && $data['category_id']){
                $query->whereHas('category', function ($query) use ($data){
                    $query->where('category.id', $data['category_id']);
                });
            }
        })->get();
        if (empty($tag)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $tag);
    }

    public static function deleteTag($data)
    {
        $tag = Tag::find($data['tag_id']);
        if (empty($tag)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$tag->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }
}